import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.ConnectionEvent;
import javax.sql.DataSource;
import java.sql.*;
import java.util.Scanner;

public class JbdcDemo1 {
    public static void main(String[] args) throws SQLException {
        //1.
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("20060304");
        //2.
        Connection connection = dataSource.getConnection();
        //3.
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入id：");
        int id = scanner.nextInt();
        System.out.print("请输入名字： ");
        String name = scanner.next();
        String sql = "insert into student values (?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,id);
        preparedStatement.setString(2,name);
        //4.
        int n = preparedStatement.executeUpdate();//用于看代码影响了几行
        System.out.println("n = " + n);
        //5.
        preparedStatement.close();
        connection.close();
    }
}
